package atn.Main;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;


import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class StyleAPI 
{
	public static Class R;
	public static int deviceW,deviceH,dpi;
	public static float xDpi,yDpi,den;
	public static void LoadSize(Activity ac)
	{
		DisplayMetrics dm=new DisplayMetrics();
		ac.getWindowManager().getDefaultDisplay().getMetrics(dm);
		deviceW=dm.widthPixels;
		deviceH=dm.heightPixels;
		den=dm.density;
		dpi=dm.densityDpi;
		xDpi=dm.xdpi;
		yDpi=dm.ydpi;
    	//Toast.makeText(ac.getApplicationContext(),"Getsize ok",Toast.LENGTH_SHORT).show();

	}
	private static class StyleItem
	{
		public static final int PIXEL=0;
		public static final int DP=1;
		public static final int PERCENT=2;
		public int wUnit,hUnit;
		public String id;
		//public String clas;
		public String width,height;
		public void Apply(Activity ac)
		{
			int wp,hp,w,h,cwp,chp;
			w=0;
			h=0;
			try
			{
				h=Integer.parseInt(height);
				w=Integer.parseInt(width);
			}
			catch(Exception ex)
			{
	        	//Toast.makeText(ac.getApplicationContext(),"Parse error",Toast.LENGTH_SHORT).show();

			}
			
			switch(wUnit)
			{
				case 1:
				{
					wp=(int)(w*xDpi/160);
					break;
				}
				case 2:
				{
					wp=(int)(w*deviceW/100);
					break;
				}
				default:
				{
					wp=w;
				}
			}
			switch(hUnit)
			{
				case 1:
				{
					hp=(int)(h*yDpi/160);
					break;
				}
				case 2:
				{
					hp=(int)(h*deviceH/100);
					break;
				}
				default:
				{
					hp=h;
				}
			}
			
			try
			{
				int rId=0;
				Class[] arrCl=R.getDeclaredClasses();
				Class sid=null;
				String temp="";
				for(Class cl:arrCl)
				{
					//temp+=cl.getName()+"-";
		        	//Toast.makeText(ac.getApplicationContext(),cl.getName(),Toast.LENGTH_LONG).show();
					if(cl.getName().contains("$id"))
					{
			        	//Toast.makeText(ac.getApplicationContext(),cl.getName(),Toast.LENGTH_LONG).show();
						sid=cl;
						//break;
					}
				}
	        	//Toast.makeText(ac.getApplicationContext(),temp,Toast.LENGTH_LONG).show();
				
				if(sid!=null)
				{
					Toast.makeText(ac.getApplicationContext(),sid.getName(),Toast.LENGTH_SHORT).show();
					rId=sid.getField(id).getInt(null);
					View v=ac.findViewById(rId);
					cwp=v.getWidth();
					chp=v.getHeight();
					if(width.length()==0)
					{
						wp=cwp;
					}
					if(height.length()==0)
					{
						hp=chp;
					}
					LinearLayout.LayoutParams l=new LinearLayout.LayoutParams(wp,hp);
					v.setLayoutParams(l);
				}
				
			}
			catch(Exception ex)
			{
	        	Toast.makeText(ac.getApplicationContext(),ex.getMessage(),Toast.LENGTH_SHORT).show();

			}
			
		}

	}
	public static void Load(Activity ac,int styleId)
	{
//		Activity ac=new Activity();
//		ac.setContentView(interfaceId);
		ArrayList<StyleItem> lstStyleItem=new ArrayList<StyleItem>();
    	//Toast.makeText(ac.getApplicationContext(),"test toast",Toast.LENGTH_LONG).show();
		LoadSize(ac);	
		try
		{
			XmlResourceParser xml=ac.getResources().getXml(styleId);
			int et=xml.next();
			int i=0;
			while(et!=XmlPullParser.END_DOCUMENT)
			{
				if(i>1)
				{
					if(et==XmlPullParser.TEXT)
					{
	                	Toast.makeText(ac.getApplicationContext(),"Text: "+xml.getName(),Toast.LENGTH_SHORT).show();
						
					}
					else if(et==XmlPullParser.START_TAG)
					{
	                	//Toast.makeText(ac.getApplicationContext(),"not text:"+xml.getName(),Toast.LENGTH_SHORT).show();
						StyleItem si=new StyleItem();
						//String t=xml.getAttributeValue(null, "w");
						si.id=xml.getAttributeValue(null,"id");
						//si.clas=xml.getAttributeValue(null,"class");
						String tw=xml.getAttributeValue(null,"width");
						String th=xml.getAttributeValue(null,"height");
						int j=0;
						for(j=0;j<tw.length();j++)
						{
							
							if(!Character.isDigit(tw.charAt(j)))
							{
								break;
							}
						}
						String twd=tw.substring(0,j);
						String twu;
						if(j<tw.length())
						{
							twu=tw.substring(j,tw.length());
						}
						else
						{
							twu="%";
						}
						twu.toLowerCase();
						if(twu.compareTo("%")==0)
						{
							si.wUnit=2;
						}
						if(twu.compareTo("px")==0)
						{
							si.wUnit=0;
						}
						if(twu.compareTo("dp")==0)
						{
							si.wUnit=1;
						}
						si.width=twd;
						for(j=0;j<th.length();j++)
						{
							
							if(!Character.isDigit(th.charAt(j)))
							{
								break;
							}
						}
						String thd=th.substring(0,j);
						String thu;
						if(j<th.length())
						{
							thu=th.substring(j,th.length());
						}
						else
						{
							thu="%";
						}
						thu.toLowerCase();
						if(thu.compareTo("%")==0)
						{
							si.hUnit=2;
						}
						if(thu.compareTo("px")==0)
						{
							si.hUnit=0;
						}
						if(thu.compareTo("dp")==0)
						{
							si.hUnit=1;
						}
						si.height=thd;
	                	//Toast.makeText(ac.getApplicationContext(),"Style id :"+si.id+"-width: "+si.width+"-height: "+si.height,Toast.LENGTH_SHORT).show();
						
						//si.selector
						si.Apply(ac);
	
					}
				}
				et=xml.next();
				i++;
			}
		}
		catch(Exception ex)
		{
			//Toast.makeText(ac.getApplicationContext(), ex.getMessage(), Toast.LENGTH_LONG);
		}
		//View v=new View(ac.getApplicationContext());
		//return v;
	}

}
